/*
* @Author: root
* @Date:   2017-11-27 21:31:52
* @Last Modified by:   root
* @Last Modified time: 2017-11-29 21:06:44
*/
'user strict'
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var _utils          = require('util/util.js');
var _cart           = require('service/cart-service.js');
var templateIndex   = require('./index.string');
var nav = require('page/common/nav/index.js');
var page = {
	data : {
		
	},
	init : function(){
		this.onload();
		this.bindEvent();
	},
	onload : function(){
		
		this.loadCart();
	},
	bindEvent : function(){
        var _this = this;
        	//商品的选择	 /取消选择
            $(document).on('click', '.cart-select', function(){
            var $this = $(this),
            	productId = $this.parents('.cart-table').data('product-id');
            // 切换选中状态
            if ($this.is(':checked')) {
            	_cart.selectProduct(productId,function(res){
            		_this.renderCart(res);
            	},function(errMsg){
            		_this.showCartError();
            	});
            }	
            //取消选中
            else {
            	_cart.unselectProduct(productId,function(res){
            	_this.renderCart(res);		
            	},function(errMsg){
            		_this.showCartError();
            	});
            }
        });

            //商品的全选 /取消全选
            $(document).on('click', '.cart-select-all', function(){
            var $this = $(this);
            // 全选
            if ($this.is(':checked')) {
            	_cart.selectAllProduct(function(res){
            		_this.renderCart(res);
            	},function(errMsg){
            		_this.showCartError();
            	});
            }	
            //取消全选
            else {
            	_cart.unselectAllProduct(function(res){
            	_this.renderCart(res);		
            	},function(errMsg){
            		_this.showCartError();
            	});
            }
        });
            //商品数量的变化
             $(document).on('click', '.count-btn', function(){

             	var $this     = $(this),
             		$pCount     = $this.siblings('.count-input'),
             		 currCount   = parseInt($pCount.val()),
             		type        = $this.hasClass('plus') ? 'plus' : 'minus',
             		 productId   = $this.parents('.cart-table').data('product-id'),
             		minCount    = 1,
             		 maxCount    = parseInt($pCount.data('max')),
             		newCount   = 0;
             	 if(type === 'plus'){
                if(currCount >= maxCount){
                    _utils.errorTips('该商品数量已达到上限');
                    return;
                }
                newCount = currCount + 1;
            }else if(type === 'minus'){
                if(currCount <= minCount){
                    return;
                }
                newCount = currCount - 1;
            }
             	//更新商品数量		
             	_cart.updateProduct({
             		productId : productId,
             		count : newCount
             	},function(res){
             		_this.renderCart(res);	
             	},function(errMsg){
             		_this.showCartError();
             	});
             });
             //删除单个商品
             $(document).on('click', '.cart-delete', function(){
             	if (window.confirm("确定要删除商品？")) {
             		var productId = $(this).parents('.cart-table').data('product-id');
             		_this.deleteCartProduct(productId);
             	};
             });
             //删除多个商品
             $(document).on('click', '.delete-selected', function(){
             	
             	if (window.confirm("确定要删除选中商品？")) {
             		var arrProductIds = [],
             		    $selecttedItem		 = $('.cart-select:checked');
             		    //循环查找选中的productIds
             		    for(var i=0,iLength  = $selecttedItem.length; i < iLength ; i++){
             		    	arrProductIds.push($($selecttedItem[i]).parents('.cart-table').data('product-id'));
             		    }
             		    if(arrProductIds.length){
             		    _this.deleteCartProduct(arrProductIds.join(','));
             		    }
             		    else {
             		    	_utils.errorTips('您还没有选中要删除的商品');
             		    }
             		
             	};
             });
              //结算
            $(document).on('click', '.btn-submit', function(){
            // 总价大于0，进行提交
            if(_this.data.carInfo && _this.data.carInfo.cartTotalPrice > 0){
                window.location.href = './order-confirm.html';
            }else{
                _utils.errorTips('请选择商品后再提交');
            }
        });
             
    },
	//加载购物车信息
	loadCart : function(){
			var _this = this;
			_cart.getCartList(function(res){
				_this.renderCart(res);
			},function(errMsg){
				_this.showCartError();
			});
	},
	//删除指定的商品,支持批量 , productId 用逗号分隔
	deleteCartProduct : function (productIds){
		var _this = this;
		_cart.deleteProduct(productIds,function(res){
			_this.renderCart(res);
		},function(errMsg){
			_this.showCartError();
		}) 
	},
	//数据匹配
	filter : function(data){
		 data.notEmpty = !!data.cartProductVoList.length;
	},
	//渲染购物车
	renderCart : function(data){
		this.filter(data);
		//缓存购物车信息
		this.data.carInfo = data;
		//生成html
		var cartHtml = _utils.renderHtml(templateIndex,data);
		$('.page-wrap').html(cartHtml);
        nav.loadCartCount();
        //通知导航条模块更新购物车数量
	},
	//显示错误信息
	showCartError : function(){
		$('.page-wrap').html('<p class="err-tip">哪里不对了，刷新下试试吧</p>');
	}
	 
		
};

$(function(){
	page.init();
});
